1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class GameManager : MonoBehaviour {
6
7     
public FairyScript fairy;
8
9     
public GameObject[] enemies;
10     
public GameObject[] rewards;
11
12     
public float spawnTime = 3f;
13     
public int numOfEnemies = 5;
14
15     
float enemyWidth = 1.5f;
16     
float cameraUpperBound = 7f; // TODO: find a better way of restricting the spawn points
17
18
19     
// Use this for initialization
20     
void Start () {
21         InvokeRepeating (
"Spawn", spawnTime, spawnTime);
22     }
23     
24     
// Update is called once per frame
25     
void Update () {
26         
27     }
28
29     
void Spawn () {
30         
if (fairy.playerLives <= 0) {
31             
return;
32         }
33
34         Vector3 pos =
new Vector3 (-enemyWidth * (numOfEnemies + 1) / 2, cameraUpperBound, 0f);
35
36         
for (int i = 0; i < numOfEnemies; i++) {
37             
int randomIndex = Random.Range (0, enemies.Length);
38             GameObject randomEnemy = enemies [randomIndex];
39             pos +=
new Vector3 (1.5f, 0, 0);
40 // Debug.Log (
"pos: " + pos);
41
42 // GameObject enemy = Instantiate (randomEnemy, pos, Quaternion.identity);

43             GameObject enemy = ObjectPooler.SharedInstance.GetPooledObject(randomEnemy.tag);
44
45             
if (enemy != null) {
46                 
// attach reward
47                 
int r = Random.Range (0, rewards.Length);
48                 GameObject randomReward = rewards [r];
49
50                 GameObject reward = ObjectPooler.SharedInstance.GetPooledObject(randomReward.tag);
51                 
if (reward != null) {
52                     enemy.GetComponent<EnemyScript> ().reward = reward;
53                 }
else {
54                     Debug.Log (
"No more rewards");
55                 }
56
57                 enemy.transform.position = pos;
58                 enemy.GetComponent<EnemyScript> ().isVelocityEnabled =
true;
59                 enemy.GetComponent<EnemyScript> ().velocity =
new Vector3 (0, -2f, 0);
60                 enemy.SetActive(
true);
61             }
62         }
63     }
64 }


Gõ tìm kiếm nhanh...